//index.js
//获取应用实例
const app = getApp();
const util = require("../../utils/util.js");
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    backgroundList: [{
      img: "http://jiangping.goobye.cn/guanggaotu2.png"
    }, {
      img: "http://jiangping.goobye.cn/guanggaotu2.png"
    }, {
      img: "http://jiangping.goobye.cn/guanggaotu2.png"
    }, ],
    styleList: [],
    bannerImg: "",
    openId: ""
  },

  // 点击开始定制
  startBtn: function(e) {
    // console.log(e)
    let that = this
    // console.log(e.target.id)
    let productId = e.currentTarget.id
    // 判断是否绑定手机号
    let phone = wx.getStorageSync('phone')
    if (phone.length == 11) {
      wx.navigateTo({
        url: 'fabricChoice/fabricChoice?productId=' + productId,
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '您尚未绑定手机号，请先绑定手机号继续执行此操作',
        confirmText: "去绑定",
        cancelText: "取消",
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../mine/bindPhonenum/bindPhonenum',
            })
          }
        }
      });
    }
    // let phoneUrl = app.globalData.baseUrl + "weixin/judgePhone"
    // let data = {
    //   openId: that.data.openId
    // }
    // util.requestLoading(phoneUrl, data, "GET").then(res => {
    //   console.log(res)
    //   if (res.data.status == 1) {

    //   } else {
    //     wx.navigateTo({
    //       url: 'fabricChoice/fabricChoice',
    //     })
    //   }
    // //   // this.setData({
    // //   //   bannerImg: res.data.adList[0].loc
    // //   // })
    // })

  },

  onLoad: function() {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        // console.log(res.windowWidth);
        // console.log(res.windowHeight);
      },
    })
    // 判断是否有openId和phone
    // console.log(app.globalData.openId)
    // console.log(app.globalData.phone)
    if (app.globalData.openId && app.globalData.phone) {
      // console.log("老用户")
    } else {
      // 登录
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId
          // console.log(res.code)
          // 获取openId
          let openidUrl = app.globalData.baseUrl + "weixin/getOpenId"
          let data = {
            js_code: res.code
          }
          util.requestLoading(openidUrl, data, "GET").then(res => {
            console.log(res.data.openid)
            wx.setStorageSync('openId', res.data.openid)
            that.setData({
              openId: res.data.openid
            })
          })
        }
      })
    }
    wx.showLoading()
    // 请求广告图列表
    let imgUrl = app.globalData.baseUrl + "weixin/adList"
    util.requestLoading(imgUrl, null, "GET").then(res => {
      // console.log(res)
      this.setData({
        bannerImg: res.data.adList[0].loc
      })
    })

    // 请求热门单品
    let hotUrl = app.globalData.baseUrl + "weixin/hotProductList"
    util.requestLoading(hotUrl, null, "GET").then(res => {
      // console.log(res)
      this.setData({
        styleList: res.data.adList
      })
    })
    wx.hideLoading()
  },


})